$(function () {
    if (editType != 'info') {
        regValidate(["#_btnDataForm"]);
    }
});

function doBtnSave() {
    if(!$("#_btnDataForm").valid()) return;//校验不通过
    var btnName = $('#btnName').val();
    var btnCode = $('#btnCode').val();
    var menuId = $('#menuId').val();
    var tabId = $('#tabId').val();
    var sortNum = $('#sortNum').val();
    if (!sortNum || sortNum == '') {
        sortNum = 999;
    }
    var postData = {
        btnName : btnName,
        btnCode : btnCode,
        menuId : menuId,
        tabId : tabId,
        sortNum : sortNum
    };
    var url = basePath + 'pfButton/addButton?t=' + new Date().getTime();
    if (editType == 'edit') {
        postData.id = oneBtnInfo.id;
        url = basePath + 'pfButton/editButton?t=' + new Date().getTime();
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        data: postData,
        success : function (data) {
            if (!data) {
                toastr.error(common_save_fail);
                return;
            }
            if (data.isSuccess) {
                toastr.success(common_save_success);
                doBack();
                return;
            }
            if (!data.isSuccess && data.errorCode == 'EXIST_CODE') {
                toastr.error('【'+btnName+'】编码已存在');
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
            return;
        }
    });
}

function doBack() {
    var menuIdStr = (searchInfo.menuId && searchInfo.menuId != '') ? ('&menuId=' + searchInfo.menuId) : '';
    var tabIdStr = (searchInfo.tabId && searchInfo.tabId != '') ? ('&tabId=' + searchInfo.tabId) : '';
    window.location.href = basePath + 'pfButton/buttonPage?status=' + searchInfo.status + menuIdStr + tabIdStr +
        '&t=' + new Date().getTime() + '&pageNo=' + searchInfo.pageNo + '&pageSize=' + searchInfo.pageSize;
}

function doCheckDataChange() {
    var data = getFromData('#_btnDataForm');
    var tabId = data.tabId;
    if (editType != 'add' && !data.tabId) {
        tabId = -1;
    }
    var checkData = {
        btnName : data.btnName,
        btnCode : data.btnCode,
        menuId : data.menuId,
        tabId : tabId,
        sortNum : data.sortNum
    };
    if (editType != 'info' && (!Compare(checkData, oneBtnInfo))) {
        swalClosePageToastr(doBack);
        return;
    }
    doBack();
}

function doChooseTab() {
    var menuId = $('#menuId').val();
    if (!menuId || menuId == '') {
        $('#tabId').val('');
        var str = '<option value="">--请选择--</option>';
        $('#tabId').html(str);
        doRefresh();
    } else {
        var postData = {
            menuId : menuId,
        };
        $.ajax({
            type: 'post',
            url: basePath + 'pfButton/selectTab?t=' + new Date().getTime(),
            dataType: 'json',
            data: postData,
            success : function (data) {
                if(data && data.result){
                    setTabSelect(data.content);
                }else if(data&&!data.result){
                    toastr.error(data.message);
                }else{
                    toastr.error('获取城市信息失败');
                }
            },
            error: function (request) {
                delayToastr('系统错误');
            }
        });
    }
}

function setTabSelect(tabList) {
    $('#tabId').val('');
    var htmlStr = '<option value="">--请选择--</option>';
    if (tabList) {
        for (var i=0; i<tabList.length; i++) {
            htmlStr += '<option value="'+tabList[i].code+'">'+tabList[i].name+'</option>';
        }
    }
    $('#tabId').html(htmlStr);
    doRefresh();
}